package com.x.erp.dao;

import com.x.erp.model.Car;
import com.x.erp.model.CarImage;
import com.x.framework.Base;
import com.x.framework.dao.BaseDao;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 车辆图片DAO
 */
@Repository
public class CarImageDao extends BaseDao {

	private final static String selectCarImageByCarIdSql = "SELECT " + 
			"eci.* " + 
			"FROM " + 
			"ERP_CAR_IMAGE eci " +
			"WHERE eci.CAR_ID = ? ";

	/**
	 * 查询车辆图片信息
	 * @param carImage
	 * @return
	 * @throws Exception 
	 */
	public List<CarImage> selectCarImageByCarId(CarImage carImage) throws Exception {
		StringBuilder sql = new StringBuilder(selectCarImageByCarIdSql);
		List<Object> params = new ArrayList<Object>();
		params.add(carImage.getCarId());
		if(StringUtils.isNotEmpty(carImage.getImageKey())) {
			sql.append("AND eci.IMAGE_KEY = ? ");
			params.add(carImage.getImageKey());
		}
		sql.append("ORDER BY eci.IMAGE_SEQ ");
		return this.queryForList(sql.toString(), CarImage.class, params);
	}

	private final static String selectCarImageListSql = "SELECT " +
			"eci.* " +
			"FROM " +
			"ERP_CAR_IMAGE eci ";

	private final static String selectCarImageByCarIdFromWhereSql = "WHERE 1=1 ";

	/**
	 * 查询车辆图片信息列表
	 * @param carImage
	 * @return
	 * @throws Exception
	 */
	public List<CarImage> selectCarImageList(CarImage carImage, Integer imageViewId) throws Exception {
		StringBuilder sql = new StringBuilder(selectCarImageListSql);
		StringBuilder whereSql = new StringBuilder(selectCarImageByCarIdFromWhereSql);
		List<Object> params = new ArrayList<Object>();
		if(imageViewId != null){
			sql.append("JOIN BASE_IMAGE_VIEW_TYPE bivt ON eci.IMAGE_KEY = bivt.IMAGE_TYPE_KEY ");
			whereSql.append("AND bivt.IMAGE_VIEW_ID = ? ");
			params.add(imageViewId);
		}
		searchCondition(whereSql, params, carImage);
//		if(StringUtils.isNotEmpty(carImage.getImageId())) {
//			whereSql.append("AND eci.IMAGE_ID = ? ");
//			params.add(carImage.getImageId());
//		}
//		if(StringUtils.isNotEmpty(carImage.getCarId())) {
//			whereSql.append("AND eci.CAR_ID = ? ");
//			params.add(carImage.getCarId());
//		}
//		if(StringUtils.isNotEmpty(carImage.getImageKey())) {
//			whereSql.append("AND eci.IMAGE_KEY = ? ");
//			params.add(carImage.getImageKey());
//		}
		whereSql.append("ORDER BY eci.IMAGE_SEQ ");
		return this.queryForList(sql.append(whereSql).toString(), CarImage.class, params);
	}
	
	private final static String selectCarImageSql = "SELECT " + 
			"eci.* " + 
			"FROM " + 
			"ERP_CAR_IMAGE eci " + 
			"WHERE 1=1 ";
	/**
	 * 查询单张车辆图片信息
	 * @param carImage
	 * @return
	 * @throws Exception
	 */
	public CarImage selectCarImage(CarImage carImage) throws Exception {
		StringBuilder sql = new StringBuilder(selectCarImageSql);
		List<Object> params = new ArrayList<Object>();
		searchCondition(sql, params, carImage);
		List<CarImage> carImages = this.queryForList(sql.toString(), CarImage.class, params);
		if(carImages !=null && carImages.size() > 0){
			return carImages.get(0);
		}
		return null;
	}
	private void searchCondition(StringBuilder sql, List<Object> params, CarImage carImage) {
		if(carImage != null){
			if(StringUtils.isNotEmpty(carImage.getImageId())) {
				sql.append("AND eci.IMAGE_ID = ? ");
				params.add(carImage.getImageId());
			}
			if(StringUtils.isNotEmpty(carImage.getCarId())) {
				sql.append("AND eci.CAR_ID = ? ");
				params.add(carImage.getCarId());
			}
			if(StringUtils.isNotEmpty(carImage.getImageKey())) {
				sql.append("AND eci.IMAGE_KEY = ? ");
				params.add(carImage.getImageKey());
			}
		}
	}


	private final static String deleteCarImageListSql = "DELETE eci FROM ERP_CAR_IMAGE eci,BASE_IMAGE_VIEW_TYPE bivt " +
			"WHERE eci.IMAGE_KEY = bivt.IMAGE_TYPE_KEY AND bivt.IMAGE_VIEW_ID = ? ";
	/**
	 * 批量删除车辆图片信息
	 * @param carImage
	 * @return
	 * @throws Exception
	 */
	public void deleteCarImageList(CarImage carImage,  Integer imageViewId) throws Exception {
		StringBuilder sql = new StringBuilder(deleteCarImageListSql);
		List<Object> params = new ArrayList<>();
		params.add(imageViewId);
		searchCondition(sql, params, carImage);
		this.execute(sql.toString(), params);
	}

	private final static String insertCarImageListSql = "INSERT INTO ERP_CAR_IMAGE VALUES ";

	/**
	 * 批量添加车辆图片信息
	 * @param carImage
	 * @param selectCarImages
	 * @return
	 * @throws Exception
	 */
	public void insertCarImageList(CarImage carImage, List<CarImage> selectCarImages) throws Exception {
		StringBuilder sql = new StringBuilder(insertCarImageListSql);
		List<Object> params = new ArrayList<>();
		if (selectCarImages == null || selectCarImages.size() ==0){
			return;
		} else {
			for (int i = 0;i < selectCarImages.size();i++){
				if (i == 0 ){
					sql.append("(?,?,?,?,?,?) ");
					params.add(Base.getUid(32));
					params.add(selectCarImages.get(i).getImageUrl());
					params.add(selectCarImages.get(i).getImageKey());
					params.add(selectCarImages.get(i).getImageSeq());
					params.add(new Date());
					params.add(carImage.getCarId());
				} else {
					sql.append(",(?,?,?,?,?,?) ");
					params.add(Base.getUid(32));
					params.add(selectCarImages.get(i).getImageUrl());
					params.add(selectCarImages.get(i).getImageKey());
					params.add(selectCarImages.get(i).getImageSeq());
					params.add(new Date());
					params.add(carImage.getCarId());
				}
			}
			this.execute(sql.toString(), params);
		}
	}
}
